package edu.hnu.cloudflight.android;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import edu.hnu.cloudflight.android.adapter.CityAdapter;
import edu.hnu.cloudflight.android.supply.DateBtnOnclick;
import edu.hnu.cloudflight.android.util.DateUtil;
import edu.hnu.cloudflight.android.util.ToastUtil;

public class FlightCourseQueryActivity extends Fragment {

	private AutoCompleteTextView startAutoCompleteTextView;
	private AutoCompleteTextView desAutoCompleteTextView;
	private ArrayAdapter<String> aAdapter;
	@SuppressWarnings("unused")
	private static final String TAG = FlightCourseQueryActivity.class.getSimpleName();

	private Button starDatetBtn;
	private Button endDateBtn;
	private Button queryBtn;
	private TextView startDateText;
	private TextView endDateText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.fragment_flightcoursequery, container, false);
		return v;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		startAutoCompleteTextView = (AutoCompleteTextView) getView().findViewById(R.id.statringautocomplete);
		desAutoCompleteTextView = (AutoCompleteTextView) getView().findViewById(R.id.destinationautocomplete);

		starDatetBtn = (Button) getActivity().findViewById(R.id.flightcoursequery_btn_id_startdate);
		endDateBtn = (Button) getActivity().findViewById(R.id.flightcoursequery_btn_id_enddate);
		queryBtn = (Button) getActivity().findViewById(R.id.queryByFlightCourse);

		startDateText = (TextView) getActivity().findViewById(R.id.flightcoursequery_textview_id_startdate);
		endDateText = (TextView) getActivity().findViewById(R.id.flightcoursequery_textview_id_enddate);

		String date = DateUtil.getNowDateToString(DateUtil.sdf1);
		startDateText.setText(date);
		endDateText.setText(date);
		starDatetBtn.setOnClickListener(new DateBtnOnclick(startDateText, getActivity()));
		endDateBtn.setOnClickListener(new DateBtnOnclick(endDateText, getActivity()));

		startAutoCompleteTextView.setThreshold(1);
		desAutoCompleteTextView.setThreshold(1);
		aAdapter = new CityAdapter(getActivity(), R.layout.list_item);
		startAutoCompleteTextView.setAdapter(aAdapter);
		desAutoCompleteTextView.setAdapter(aAdapter);

		queryBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String start = startAutoCompleteTextView.getText().toString();
				String end = desAutoCompleteTextView.getText().toString();
				if(start==null||end==null||"".equals(start)||"".equals(end)){
					ToastUtil.makeToastForShort(getActivity(), R.string.flightcoursequery_tos_error);
					return;
				}
				String startDate = startDateText.getText().toString();
				String endDate = endDateText.getText().toString();
				Intent intent = new Intent(getActivity(),FlightCourseResultActivity.class);
				intent.putExtra("start", start);
				intent.putExtra("end", end);
				intent.putExtra("startDate", startDate);
				intent.putExtra("endDate", endDate);
				startActivity(intent);

			}
		});
	}

}
